﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Enemy : MonoBehaviour {
    Rigidbody2D rigid;
    private Animator _animator;
    Vector2 enemyPos;//敌人位置坐标
    float groPosY;//敌人在地上的Y轴坐标位置
    float posX,posY;//此时标位置
    bool overBol=false;
    private GameObject hero;//主角对象
    bool FacingRight = true;//敌人方向
    bool attedBool = false;
    float tranDis;
    AnimatorStateInfo infoAnim;

    public float HP = 50;
    public Transform head;
    public float bj = 0;//被击数
    public float timer, bjTime;
    public enum STATE
    {
        IDLE,//静止状态 0
        ATTACK,//攻击状态
        ATTACKED,//被攻击 11,12
        Over,//HP为0 5
    }
    public STATE e_state;
    public enum ATTSTATE
    {
        WALK,//走
        RUN,//跑 2
        PA,//敌人普通攻击 3
    }
    public ATTSTATE e_att;

    // Use this for initialization
    void Start () {
        hero = GameObject.Find("Player");//得到主角对象
        _animator = GetComponent<Animator>();//获取动画控制器
        rigid = GetComponent<Rigidbody2D>();//获取刚体组件
        enemyPos = GetComponent<Transform>().position;//获取敌人坐标
        groPosY = enemyPos.y;//储存当前位置
        init();
    }
    // Update is called once per frame
    void Update () {

        infoAnim = _animator.GetCurrentAnimatorStateInfo(0);
        tranDis = Vector2.Distance(transform.position, hero.transform.position);//敌人与主角的距离差值
        EnemyAnimator();
        if (!attedBool)
        {
            if (tranDis < 4 ) 
            {
                e_state = STATE.ATTACK;
            }
            else
            {
                e_state = STATE.IDLE;
            }
        }

        //Debug.Log("groPosY:" + groPosY);
        timer += Time.deltaTime;
        OnGround();
    }
    public void FixedUpdate()
    {
        
    }

    void EnemyAnimator()
    {
        switch (e_state)
        {
            case STATE.IDLE:
                _animator.SetInteger("state", 0);
                attedBool = false;//被击关
                Debug.Log("ting");
                break;            
            case STATE.ATTACK:
                float distance = transform.position.x - hero.transform.position.x;//获取敌人与主角X轴坐标差
                int type = Random.Range(0, 10); //取得0~10之间的随机数
                

                if (tranDis > 1)
                {
                    e_att = ATTSTATE.RUN;
                }
                else
                {
                    if(type>0&&type<2 && infoAnim.normalizedTime >= 1f)
                    {
                        e_att = ATTSTATE.PA;
                    }
                    if(type>2 && infoAnim.normalizedTime >= 1f)
                    {
                        e_att = ATTSTATE.WALK;
                    }
                }
                //攻击状态
                switch (e_att)
                {
                    case ATTSTATE.WALK:
                        _animator.SetInteger("state", 0);
                        break;
                    case ATTSTATE.RUN:
                        _animator.SetInteger("state", 2);
                        if (distance > 0)//敌人在主角右面
                        {
                            transform.Translate(-Time.deltaTime * 2, 0, 0);//敌人向左移动
                            if (FacingRight)//转身
                            {
                                TrunBack();
                            }
                        }
                        if (distance < 0)//敌人在主角左面
                        {
                            transform.Translate(Time.deltaTime * 2, 0, 0);//敌人向右移动
                            if (!FacingRight)//转身
                            {
                                TrunBack();
                            }
                        }
                        //Vector2 dir = hero.transform.position - transform.position;//主角与敌人距离正负值
                        //transform.Translate(dir.normalized * Time.deltaTime);//敌人向主角移动
                        break;
                    case ATTSTATE.PA:
                        _animator.SetInteger("state", 3);
                        if (distance > 0 && FacingRight) //敌人在主角右面
                        {
                            TrunBack();
                        }
                        if (distance < 0 && !FacingRight) //敌人在主角左面
                        {
                            TrunBack();
                        }
                        break;
                    default:
                        break;
                }
                break;
            case STATE.ATTACKED:
                attedBool = true;//被击开
                bjTime += Time.deltaTime;
                if (bjTime > 1)//受击时间大于1秒，还原为正常状态
                {
                    bj = 0;
                    e_state = STATE.IDLE;
                    bjTime = 0;
                    attedBool = false;//被击开
                }
                if (HP <= 0) 
                {
                    overBol = true;
                    e_state = STATE.Over;
                }
                if (bj % 2 == 0)
                {
                    _animator.SetInteger("state", 11);
                }
                if (bj % 2 == 1)
                {
                    _animator.SetInteger("state", 12);
                }
                break;
            case STATE.Over:
                _animator.SetInteger("state", 5);
                // 判断动画是否播放完成
                if (infoAnim.normalizedTime >= 1.0f)
                {
                    //播放完毕，要执行的内容
                    Twinkel();
                }
                Destroy(gameObject,3f);
                break;
            default:
                break;
        }
    }

    public void init()
    {
        FacingRight = true;//敌人初始方向为正方向（右）
        timer = 0;
        bjTime = 0f;
        rigid.gravityScale = 0;//去掉重力
        //rigid.isKinematic = false;//false表示模拟真实物理引擎

    }

    //使物体闪烁
    void Twinkel()
    {
        gameObject.GetComponent<Renderer>().enabled = (timer * 4 % 2 < 1) ? true : false;
    }

    //人物转身
    void TrunBack()
    {
        FacingRight = !FacingRight;
        Vector3 theScale = this.transform.localScale;
        theScale.x *= -1;  //物体镜像翻转
        transform.localScale = theScale;
    }

    //被攻击
    private void OnTriggerEnter2D(Collider2D collision)
    {
        if (collision.gameObject.tag.Equals("Attack"))
        {
            Debug.Log("Dong!Trrig进入");
            bj++;
            HP = HP - 10;
            bjTime = 0;
            e_state = STATE.ATTACKED;
        }
    }

    //记录地面
    void OnGround()
    {
        enemyPos = GetComponent<Transform>().position;//获取敌人坐标
        posY = enemyPos.y;
        if (posY < groPosY)
        {
            rigid.gravityScale = 0;
            transform.position = new Vector2(enemyPos.x, groPosY);
        }
    }
}
